# coding: utf-8
# Author: zhenda

from flask import Flask
from settings import map_config
from comment.models import db


# 负责创建app对象
def create_app(config_key):
    app = Flask(__name__)
    # 加载项目配置
    app.config.from_object(map_config.get(config_key))
    # 初始化限制器
    from comment.utils.limiter import limiter
    limiter.init_app(app)
    # 加载日志处理
    from comment.utils.logging import create_logger
    create_logger(app)
    # 初始化 sqlalchemy
    db.init_app(app)
    # 连接redis
    from comment.utils.shopping_redis import redis_client
    redis_client.init_app(app)
    # 添加请求钩子
    from comment.utils.request_wares import jwt_request_authorization
    app.before_request(jwt_request_authorization)  # 所有请求都生效
    # 加载蓝图
    from Shopping.resources.user import user_bp
    from Shopping.resources.cart import cart_bp
    from Shopping.resources.address import address_bp
    from Shopping.resources.coupon import coupon_bp
    from Shopping.resources.goods import goods_bp
    from Shopping.resources.index import index_bp
    from Shopping.resources.order import order_bp
    app.register_blueprint(user_bp)
    app.register_blueprint(cart_bp)
    app.register_blueprint(address_bp)
    app.register_blueprint(coupon_bp)
    app.register_blueprint(goods_bp)
    app.register_blueprint(index_bp)
    app.register_blueprint(order_bp)

    return app


